<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="mobilePhone">
      <el-input v-model="dataForm.mobilePhone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="身份证" prop="idCard">
      <el-input v-model="dataForm.idCard" placeholder="身份证"></el-input>
    </el-form-item>
    <el-form-item label="QQ" prop="qq">
      <el-input v-model="dataForm.qq" placeholder="QQ"></el-input>
    </el-form-item>
    <el-form-item label="自我评价" prop="selfIntroduction">
      <el-input v-model="dataForm.selfIntroduction" placeholder="自我评价"></el-input>
    </el-form-item>
    <el-form-item label="爱好" prop="hobby">
      <el-input v-model="dataForm.hobby" placeholder="爱好"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="个人照" prop="imageUrl">
      <el-input v-model="dataForm.imageUrl" placeholder="个人照"></el-input>
    </el-form-item>
    <el-form-item label="登录id" prop="loginId">
      <el-input v-model="dataForm.loginId" placeholder="登录id"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="公司代号" prop="corpCode">
      <el-input v-model="dataForm.corpCode" placeholder="公司代号"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除" prop="deleted">
      <el-input v-model="dataForm.deleted" placeholder="逻辑删除"></el-input>
    </el-form-item>
    <el-form-item label="" prop="version">
      <el-input v-model="dataForm.version" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          sex: '',
          age: '',
          mobilePhone: '',
          address: '',
          idCard: '',
          qq: '',
          selfIntroduction: '',
          hobby: '',
          email: '',
          imageUrl: '',
          loginId: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: '',
          corpCode: '',
          deleted: '',
          version: ''
        },
        dataRule: {
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          mobilePhone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '身份证不能为空', trigger: 'blur' }
          ],
          qq: [
            { required: true, message: 'QQ不能为空', trigger: 'blur' }
          ],
          selfIntroduction: [
            { required: true, message: '自我评价不能为空', trigger: 'blur' }
          ],
          hobby: [
            { required: true, message: '爱好不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          imageUrl: [
            { required: true, message: '个人照不能为空', trigger: 'blur' }
          ],
          loginId: [
            { required: true, message: '登录id不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          corpCode: [
            { required: true, message: '公司代号不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/dep/ruser/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.rUser.name
                this.dataForm.sex = data.rUser.sex
                this.dataForm.age = data.rUser.age
                this.dataForm.mobilePhone = data.rUser.mobilePhone
                this.dataForm.address = data.rUser.address
                this.dataForm.idCard = data.rUser.idCard
                this.dataForm.qq = data.rUser.qq
                this.dataForm.selfIntroduction = data.rUser.selfIntroduction
                this.dataForm.hobby = data.rUser.hobby
                this.dataForm.email = data.rUser.email
                this.dataForm.imageUrl = data.rUser.imageUrl
                this.dataForm.loginId = data.rUser.loginId
                this.dataForm.createBy = data.rUser.createBy
                this.dataForm.createTime = data.rUser.createTime
                this.dataForm.updateBy = data.rUser.updateBy
                this.dataForm.updateTime = data.rUser.updateTime
                this.dataForm.corpCode = data.rUser.corpCode
                this.dataForm.deleted = data.rUser.deleted
                this.dataForm.version = data.rUser.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/dep/ruser/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'sex': this.dataForm.sex,
                'age': this.dataForm.age,
                'mobilePhone': this.dataForm.mobilePhone,
                'address': this.dataForm.address,
                'idCard': this.dataForm.idCard,
                'qq': this.dataForm.qq,
                'selfIntroduction': this.dataForm.selfIntroduction,
                'hobby': this.dataForm.hobby,
                'email': this.dataForm.email,
                'imageUrl': this.dataForm.imageUrl,
                'loginId': this.dataForm.loginId,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime,
                'corpCode': this.dataForm.corpCode,
                'deleted': this.dataForm.deleted,
                'version': this.dataForm.version
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
